import {HTTP_PATH} from './serverPath.js'

let BASEURL = HTTP_PATH // 调整当前环境

/*
* 全局请求封装
* @param path 请求路径
* @param method 请求类型(GET/POST/DELETE等)
* @oaram data 请求体数据
* @param loading 请求未完成是是否显示加载中，默认为true
*/

// 无token时发送请求函数
function noTokenRequest(path, method, data) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASEURL + path,
            method: method,
            data: data,
            timeout: 5000,
            success(response) {
                // console.log('%c响应拦截：', ' background:green', response);
                /* if (response.data.code === 3001) {
                    // logout()
                } */
                /* if (response.data.code !== 20) {
                    uni.showToast({
                        icon: "none",
                        duration: 4000,
                        title: response.data.msg
                    });
                } */
                resolve(response.data);
            },
            fail(err) {
                uni.showToast({
                    icon: "none",
                    title: '服务响应失败'
                });
                console.error(err);
                reject(err);
            },
            complete() {
                // 请求完成后清除定时器
                uni.hideLoading();
            }
        });
    });
}

// 有token时发送请求函数
function tokenRequest(path, method, data, token) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASEURL + path,
            method: method,
            data: data,
            timeout: 10000,
            header: {
                "token":  token
            },
            success(response) {
                if (response.data.code === 40101) {
                    // logout()
                }
                resolve(response.data);
            },
            fail(err) {
                uni.showToast({
                    icon: "none",
                    title: '服务响应失败'
                });
                console.error(err);
                reject(err);
            },
            complete() {
                uni.hideLoading();
            }
        });
    });
}

export default (path, method, data = {}) => {
    // 获取存储token
    const token = uni.getStorageSync("token");

    //根据token进行调用函数
    if (token != '') {
        return tokenRequest(path, method, data, token)
    } else {
        return noTokenRequest(path, method, data)
    }
};

export function get(url, param) {
    const token = uni.getStorageSync("token");
    if (token !== '') {
        return tokenRequest(url, 'GET', param, token)
    } else {
        return noTokenRequest(url, 'GET', param)
    }
}

export function getNotParam(url) {
    const token = uni.getStogrageSync("token");
    if (token !== '') {
        return tokenRequest(url, 'GET', null, token)
    } else {
        return noTokenRequest(url, 'GET', null)
    }
}

export function getPath(url) {
    const token = uni.getStorageSync("token");
    if (token !== '') {
        return tokenRequest(url, 'GET', null, token)
    } else {
        return noTokenRequest(url, 'GET', null)
    }
}

export function post(url, param) {
    const token = uni.getStorageSync("token");
    if (token !== '') {
        return tokenRequest(url, 'post', param, token)
    } else {
        return noTokenRequest(url, 'post', param)
    }
}
